import java.lang.reflect.*;

class ReflectionOrNot 
{

    public void run() 
    {
        try {
            Thread.currentThread().sleep( 0 );
        }
        catch( InterruptedException ie ){
        }
    }

    public static void main( String ... args ) throws Exception 
    {
        for (int i = 1; i <= 5; i++) {
            runTest();
            System.out.println("Test " + i + " finished \n");
        }
    }

    public static void runTest() throws Exception
    {

        ReflectionOrNot ron = new ReflectionOrNot();
        int max = 1000000;

        long start = System.currentTimeMillis();

        for( int i = 0 ; i < max ; i++ ) {
            ron.run();
        }
        System.out.println( "Direct access took: " + ( System.currentTimeMillis() - start ) );

        Method m = ReflectionOrNot.class.getDeclaredMethod("run");
        start = System.currentTimeMillis();
        for( int i = 0 ; i < max ; i++ ) {
            m.invoke( ron );
        }

        System.out.println( "Reflection    Took: " + ( System.currentTimeMillis() - start ) );

        start = System.currentTimeMillis();
        for( int i = 0 ; i < max ; i++ ) {
            m = ReflectionOrNot.class.getDeclaredMethod("run");
            m.invoke( ron );
        }
        System.out.println( "Lookup + Reflect  : " + ( System.currentTimeMillis() - start ) );
    }
}
